package com.zx.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.Delay;
import com.zx.cloud.RandomlyThrowsException;
import com.zx.cloud.Shift;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.controller.client.ProviderClient;
import com.zx.cloud.domain.Client;
import com.zx.cloud.domain.User;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.util.Page;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Alex
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGER_V1_PREFIX+"/user", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class UserController {
	
    @Autowired
    private ProviderClient providerClient;
    
    @Delay
    @RandomlyThrowsException
    @ApiOperation(value = "获取用户", notes = "")
    @RequestMapping(value = "/find", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
    public Response findUser() {
    	final ObjectDataResponse<Page<Object>> datas = providerClient.findUser();
    	if (datas == null) {
    		Shift.fatal(StatusCode.USER_NOT_EXISTS,"用户登录不存在");
    	}
    	return datas;
    }
    
    @RandomlyThrowsException
	@ApiOperation(value = "用户管理-用户列表", notes = "用户管理-用户列表")
	@RequestMapping(value = "/getUserList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Object>> getUserList(@CurrentUserId Long userId,@RequestBody User user){
		final ObjectDataResponse<Page<Object>> datas = providerClient.page(user);
    	return datas;
	}
    
	@ApiOperation(value = "用户管理-新增用户", notes = "用户管理-新增用户")
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> addUser(@RequestBody  User user) {
    	final ObjectDataResponse<Object> datas = providerClient.insert(user);
    	return datas;
    }

	@ApiOperation(value = "查询会员信息", notes = "查询会员信息")
	@RequestMapping(value = "/queryClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Client>> queryClient(@RequestBody Client client){
		return providerClient.queryClient(client);
	}
    
	@ApiOperation(value = "用户管理-新增用户", notes = "用户管理-新增用户")
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> updateUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
    	final ObjectDataResponse<Object> datas = providerClient.updateUser(user);
    	return datas;
    }
    
	@ApiOperation(value = "用户管理-删除用户", notes = "用户管理-删除用户")
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> deleteUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		final ObjectDataResponse<Object> datas = providerClient.deleteUser(user);
    	return datas;
	}
	
	@ApiOperation(value = "用户管理-禁用用户", notes = "用户管理-禁用用户")
	@RequestMapping(value = "/banUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> banUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		final ObjectDataResponse<Object> datas = providerClient.banUser(user);
    	return datas;
	}
	@ApiOperation(value = "用户管理-启用用户", notes = "用户管理-启用用户")
	@RequestMapping(value = "/startUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> startUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		final ObjectDataResponse<Object> datas = providerClient.startUser(user);
    	return datas;
	}
    
	@ApiOperation(value = "用户管理-查看用户", notes = "用户管理-查看用户")
	@RequestMapping(value = "/getUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> getUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
		final ObjectDataResponse<Object> datas = providerClient.getUser(user);
    	return datas;
	}
	
	@ApiOperation(value = "用户管理-根据账号查看用户", notes = "用户管理-根据账号查看用户")
	@RequestMapping(value = "/accountQuery", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> accountQuery(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
		final ObjectDataResponse<Object> datas = providerClient.accountQuery(user);
    	return datas;
	}
	
	@ApiOperation(value = "用户管理-重置密码", notes = "用户管理-重置密码")
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> resetPassword(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		final ObjectDataResponse<Object> datas = providerClient.resetPassword(user);
    	return datas;
	}
//
//    @Delay
//    @RandomlyThrowsException
//    @ApiOperation(value = "根据ID获取用户", notes = "")
//    @RequestMapping(value = "/users/{userId}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    public ObjectDataResponse<User> findUser(@PathVariable Long userId) {
//        final User user = userService.get(userId);
//        if (user == null) {
//            Shift.fatal(StatusCode.USER_NOT_EXISTS);
//        }
//        return new ObjectDataResponse<>(user);
//    }
//
//    @Delay
//    @RandomlyThrowsException
//    @ApiOperation(value = "获取全部用户", notes = "")
//    @RequestMapping(value = "/users", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    public ObjectCollectionResponse<User> findAll() {
//        final List<User> userList = userService.findAll(0, 10000);
//        return new ObjectCollectionResponse<>(userList);
//    }

}
